package com.boco.modules.fdoc.web.sign;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.hibernate.tool.schema.extract.spi.InformationExtractor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.boco.common.constants.BusinessConstants;
import com.boco.common.enums.ApiStatusEnum;
import com.boco.common.json.BaseJsonVo;
import com.boco.common.persistence.Page;
import com.boco.common.utils.JsonUtils;
import com.boco.common.utils.NumberUtils;
import com.boco.common.utils.StringUtils;
import com.boco.modules.fdoc.model.surrender.SurrenderRequestEntity;
import com.boco.modules.fdoc.service.DocService;
import com.boco.modules.fdoc.service.SigServicepackService;
import com.boco.modules.fdoc.service.surrender.SurrenderRequestService;
import com.boco.modules.fdoc.vo.DoctorDetailVo;
import com.boco.modules.fdoc.vo.ServicepackVo;
import com.boco.modules.fdoc.vo.surrender.SignInfoVo;
import com.boco.modules.fdoc.vo.surrender.SurrenderRequestVo;

/**
 * Description 解约审核
 * @author lzz
 * @date 2017年11月28日 下午3:47:56
 */
@Controller
@RequestMapping(value="/audit",produces = "application/json;charset=UTF-8")
public class SurrenderAudit {
	
	@Resource
	SurrenderRequestService requestService;
	@Resource
	SigServicepackService packService;
	@Resource
	DocService docSerivice;
	
	/**
	 * 跳转审核主页
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/showPage",method = RequestMethod.GET)
	public String showPack(HttpServletRequest request, Model model){
		
		return "/sign/surrenderAudit";
		
	}
	
	/**
	 * 获取请求列表
	 * @param request
	 * @param model
	 * @param originator
	 * @param status
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryRequestList",method = RequestMethod.GET)
	public String queryRequestList(HttpServletRequest request, Model model,String originator,String status,
			Integer pageIndex,Integer pageSize){
		
		SurrenderRequestVo searchVo=new SurrenderRequestVo();
		if(!"0".equals(originator)){
			searchVo.setOriginator(originator);
		}
		if(!"0".equals(status)){
			searchVo.setStatus(status);
		}
		
		Integer count=requestService.getRequestCount(searchVo);
		Page<SurrenderRequestVo> page=new Page<>(pageIndex, pageSize);
		searchVo.setPage(page);
		List<SurrenderRequestVo> list=requestService.getRequestList(searchVo);
		
		Integer maxReasonValue=requestService.getMaxReasonValue();
		for(SurrenderRequestVo sVo:list){
			//处理解约原因
			List<Integer> reasonList=NumberUtils.bitandArray(sVo.getReasonValue()==null?0:sVo.getReasonValue());
			String reasonAll="";
			for(Integer a:reasonList){
				if(a==8){
					reasonAll=reasonAll+sVo.getReasonOther();
				}else{
					reasonAll=reasonAll+requestService.getReasonByValue(a)+"；";
				}
			}
			if(reasonAll.length()>20){
				reasonAll=reasonAll.replace(reasonAll.substring(10), "……");
				
			}
			sVo.setReasonAll(reasonAll);
			//处理时间
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			String requestDate=sdf.format(sVo.getRequestTime());
			sVo.setRequestDate(requestDate);
			sVo.setTeamName(sVo.getDocName()+"的团队");
			
			Map<String, Object> requestMap=new HashMap<String, Object>();
			requestMap.put("id", sVo.getId());
			requestMap.put("status", sVo.getStatus());
			sVo.setRequestMap(requestMap);
			
		}
		
		return JsonUtils.getJson(BaseJsonVo.pageList(list, count),BusinessConstants.JSON_ALL);
	}
	
	/**
	 * 请求处理
	 * @param request
	 * @param model
	 * @param id
	 * @param status
	 * @return
	 */
	@RequestMapping(value = "/requestHandling",method = RequestMethod.GET)
	public String requestHandling(HttpServletRequest request, Model model,Integer id,String status){
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		SurrenderRequestEntity requestEntity=requestService.getRequestById(id);
		
		SignInfoVo inforVo=new SignInfoVo();
		//获取签约表和个人的基础信息
		if("3".equals(status))
		{
			inforVo=requestService.getSurrenderedById(id);
		}else{
			//根据请求ID获取签约ID
			Integer sigId=requestService.getSignIdByPersonId(requestEntity.getPersonId());
			
			inforVo=requestService.getOtherById(sigId);
		}
		
		inforVo.setId(id);
		inforVo.setStatus(status);
		inforVo.setPersonId(requestEntity.getPersonId());
		inforVo.setResidentMobile(requestEntity.getResidentMobile());
		inforVo.setDocMobime(requestEntity.getDocMobile());
		inforVo.setDocTeamId(requestEntity.getDocTeamId());
		inforVo.setRequestTimeStr(sdf.format(requestEntity.getRequestTime()));
		
		//处理解约原因
		List<Integer> reasonList=NumberUtils.bitandArray(requestEntity.getReasonValue()==null?0:requestEntity.getReasonValue());
		String reasonAll="";
		for(Integer a:reasonList){
			if(a==8){
				reasonAll=reasonAll+requestEntity.getReasonOther();
			}else{
				reasonAll=reasonAll+requestService.getReasonByValue(a)+"；";
			}
		}
		if(reasonAll.length()>10){
			reasonAll.replace(reasonAll.substring(10), "……");
		}
		inforVo.setReason(reasonAll);
		
		//处理服务包
		List<Integer> packValueList=NumberUtils.bitandArray(inforVo.getServicePackValue()==null?0:inforVo.getServicePackValue());
		List<Map<String, Object>> servicePackList=new ArrayList<Map<String, Object>>();
		Double priceOfYear=0.0;
		for(Integer a:packValueList){
			Map<String, Object> packMap=new HashMap<String, Object>();
			ServicepackVo servicepackVo=packService.getVoByValue(a);
			packMap.put("packName", servicepackVo.getPackName());
			packMap.put("lastYear", inforVo.getLastYear());
			packMap.put("packPrice", servicepackVo.getPackPrice());
			if("1".equals(servicepackVo.getPackType())){
				packMap.put("packType", "基础服务");
			}else{
				packMap.put("packType", "增值服务");
			}
			servicePackList.add(packMap);
			priceOfYear+=servicepackVo.getPackPrice();
		}
		inforVo.setServicePackList(servicePackList);
		
		if(inforVo.getLastYear()==null|| "".equals(inforVo.getLastYear())){
			inforVo.setLastYear("3");
		}
		
		if(inforVo.getLastYear()==null|| "".equals(inforVo.getLastYear())){
			inforVo.setPriceAll(priceOfYear*3);
		}else {
			inforVo.setPriceAll(priceOfYear*Integer.valueOf(inforVo.getLastYear()));
		}
		
		
		//处理医生列表
		List<DoctorDetailVo> docVoList=docSerivice.getDoctorTeamMemberByTeamId(requestEntity.getDocTeamId());
		List<Map<String, Object>> docList=new ArrayList<Map<String, Object>>();
		String teamIntroduction="";
		for(DoctorDetailVo docVo:docVoList){
			Map<String, Object> docMap=new HashMap<String, Object>();
			docMap.put("docName", docVo.getDocName());
			docMap.put("docType", docVo.getDocType());
			docMap.put("leaderFlag", docVo.getLeaderFlag());
			docMap.put("docImg", docVo.getImg());
			
			docList.add(docMap);
			if(StringUtils.isNoneEmpty(docVo.getIntroduction())){
				teamIntroduction=teamIntroduction+docVo.getDocName()+"："+docVo.getIntroduction()+"。";
			}
		}
		inforVo.setDocList(docList);
		inforVo.setTeamIntroduction(teamIntroduction);
		
		System.out.println(JsonUtils.getJson(inforVo,BusinessConstants.JSON_ALL));
		if("1".equals(requestEntity.getStatus())){
			model.addAttribute("infor", inforVo);
			return "/sign/handSurrender";
		}else{
			inforVo.setAuditRemark(requestEntity.getAuditRemark());
			inforVo.setAuditTimeStr(sdf.format(requestEntity.getAuditTime()));
			
			model.addAttribute("infor", inforVo);
			return "/sign/lookSurrender";
		
		}
	}
	
	/**
	 * 拒绝·拒绝节约
	 * @param request
	 * @param model
	 * @param id
	 * @param personId
	 * @param status
	 * @param auditRemark
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/personSurrender",method = RequestMethod.POST)
	public String personSurrender(HttpServletRequest request, Model model,Integer id,String personId,String status,String auditRemark){
		
		
		if("2".equals(status)){
			//状态为2时拒绝解约
			SurrenderRequestEntity requestEntity=new SurrenderRequestEntity();
			requestEntity.setId(id);
			requestEntity.setStatus(status);
			requestEntity.setAuditTime(new Date());
			requestEntity.setAuditRemark(auditRemark);
			
			int flag=requestService.refuseSurrender(requestEntity);
			
			if(flag>0){
				return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
			}else{
				return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
			}
		}else if("3".equals(status)){
			//状态为3时同意解约
			SurrenderRequestEntity requestEntity=new SurrenderRequestEntity();
			requestEntity.setId(id);
			requestEntity.setPersonId(personId);
			requestEntity.setStatus(status);
			requestEntity.setAuditTime(new Date());
			requestEntity.setAuditRemark(auditRemark);
			
			int flag=requestService.agreeSurrender(requestEntity);
			
			if(flag>0){
				return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
			}else{
				return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
			}
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
		
	}
}
